package com.sykuku.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sykuku.common.BaseContext;
import com.sykuku.common.R;
import com.sykuku.pojo.Employee;
import com.sykuku.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
/*
1. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
2. 根据页面提交的用户名username查询数据库中员工数据信息
3. 如果没有查询到, 则返回登录失败结果
4. 密码比对，如果不一致, 则返回登录失败结果
5. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
6. 登录成功，将员工id存入Session, 并返回登录成功结果
*/

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2. 根据页面提交的用户名username查询数据库中员工数据信息

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<Employee>();
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lambdaQueryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if (emp == null) {
            return R.error("用户名或密码错误");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if (!emp.getPassword().equals(password)) {
            return R.error("用户名或密码错误");
        }

        //5. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        log.info("登录请求中当前线程为{}",Thread.currentThread().getId());

        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前登录员工的id

        HttpSession session = request.getSession();
        session.invalidate();

        return R.success("退出成功");
    }


    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {

        log.info("起始页{}每页显示数据{}分页查询条件{}",page,pageSize,name);
        //构造分页构造器
        Page<Employee> pages = new Page<>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //添加过滤条件 判断非空
        if (!StringUtils.isEmpty(name)){
            lqw.like(Employee::getName, name);
        }

        //添加排序条件
        //查询对象
        Page<Employee> employeePage = employeeService.page(pages, lqw);

        return R.success(employeePage);

    }

    @PostMapping
    public R<Employee> add(@RequestBody Employee employee, HttpServletRequest request) {
        log.info("用户开始添加员工{}",employee);
        String password = "123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //还需要添加添加人和添加修改人的id
        Long userId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(userId);
        employee.setUpdateUser(userId);

        employeeService.save(employee);

        return R.success(employee);

    }

    @PutMapping
    public R<String> updateById(@RequestBody Employee employee,HttpServletRequest request) {
        log.info("修改消息employee的值{}",employee.toString());
        //获取登录对象的id设置修改人
        Long userId = (Long) request.getSession().getAttribute("employee");
        log.info("用户开始修改用户信息id为{}",userId);
        log.info("修改请求中当前线程为{}",Thread.currentThread().getId());
        //设置修改时间


        employeeService.updateById(employee);

        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable Long id){

        Employee employee = employeeService.getById(id);

        return R.success(employee);
    }

}